package com.example.myapplication.viewmodel;


import android.os.Bundle;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;

import com.example.myapplication.base.BaseActivity;

public class MyViewModelActivity extends BaseActivity {

    private int count = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setGravity(Gravity.CENTER);

        setContentView(textView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));


        ViewModelProvider viewModelProvider = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory());
        final MyViewModel myViewModel = viewModelProvider.get(MyViewModel.class);

        textView.setText(String.valueOf(myViewModel.count));
        textView.setOnClickListener(
                view -> {
                    count++;
                    myViewModel.count = count;
                    textView.setText(String.valueOf(myViewModel.count));
                }
        );
    }
}
